declare var tt;
declare var wx;
import { _decorator, Component, Node } from 'cc';
import { AdMgr } from './AdMgr';
import { Global } from '../Global';
const { ccclass, property } = _decorator;

@ccclass('LocalStorageMgr')
/**
 * 本地数据缓存管理
 */
export class LocalStorageMgr extends Component {

    private static _instance: LocalStorageMgr;

    public static get Instance() {
        if (!this._instance) {
            this._instance = new LocalStorageMgr();
        }
        return this._instance;
    }

    //缓存key
    private LOCAL_STORAGE_KEY: string = "clear_brick_game";

    //缓存数据
    public LOCAL_STORAGE_DATA: any = {
        GAME_COIN: 1000,
        GAME_LEVEL: 0,
        GAME_SCORE: 0,
        CHANGE_SCORE: 0,
        NORMAL_SCORE: 0,
        TIME_SCORE: 0
    };

    /**
     * 初始化
     */
    public Init() {
        if (this.GetLocalStroage() != null) {
            this.LOCAL_STORAGE_DATA = this.GetLocalStroage();
        }

        console.log("本地缓存数据", this.LOCAL_STORAGE_DATA)
    }

    /**
     * 更新本地缓存
     */
    public UpdateLocalStroage() {
        if (Global.IsWx) { wx.setStorageSync(this.LOCAL_STORAGE_KEY, JSON.stringify(this.LOCAL_STORAGE_DATA)); return; }

        if (Global.IsDy) { tt.setStorageSync(this.LOCAL_STORAGE_KEY, JSON.stringify(this.LOCAL_STORAGE_DATA)); return; }

        localStorage.setItem(this.LOCAL_STORAGE_KEY, JSON.stringify(this.LOCAL_STORAGE_DATA));
    }

    /**
     * 获取本地缓存
     */
    public GetLocalStroage() {
        if (Global.IsWx) { return wx.getStorageSync(this.LOCAL_STORAGE_KEY) == "" ? null : JSON.parse(wx.getStorageSync(this.LOCAL_STORAGE_KEY)) }

        if (Global.IsDy) { return tt.getStorageSync(this.LOCAL_STORAGE_KEY) == "" ? null : JSON.parse(tt.getStorageSync(this.LOCAL_STORAGE_KEY)) }

        return JSON.parse(localStorage.getItem(this.LOCAL_STORAGE_KEY));
    }




}


